#!/bin/env python
# -*- encoding:utf-8 -*-
"""
    fenghuangscan mssql弱口令检查
    ~~~~~~~~~~~~~~~~~~~~

    主函数
    :author = 'wilson'
"""
import time
import _mssql # for pyinstaller
import threading

from comm.printers import printPink, printGreen
from multiprocessing.dummy import Pool
import pymssql


class MyPoc(object):
    def __init__(self, c):
        self.config = c
        self.lock = threading.Lock()
        self.result = []
        self.lines = self.config.file2list("conf/mssql.conf")

    def mssql_connect(self, ip, username, password, port):
        """
        mssql连接
        :param ip:
        :param username:
        :param password:
        :param port:
        :return:
        """
        crack = 0
        try:
            db = pymssql.connect(host=str(ip) + ':' + str(port), user=username, password=password)
            if db:
                crack = 1
            db.close()
        except Exception, e:
            self.lock.acquire()
            print "%s sql service 's %s:%s login fail " % (ip, username, password)
            self.lock.release()
        return crack

    def mssq1(self, ip, port):
        """
        爆破
        :param ip:
        :param port:
        :return:
        """
        try:
            for data in self.lines:
                username = data.split(':')[0]
                password = data.split(':')[1]
                flag = self.mssql_connect(ip, username, password, port)
                if flag == 2:
                    break
                if flag == 1:
                    self.lock.acquire()
                    printGreen("%s mssql at %s has weaken password!!-------%s:%s\r\n" % (ip, port, username, password))
                    self.result.append(
                        "%s mssql at %s has weaken password!!-------%s:%s\r\n" % (ip, port, username, password))
                    self.lock.release()
                    break
        except Exception, e:
            pass

    def run(self, ipdict, threads, filename):
        """
        主函数
        :param ipdict:
        :param threads:
        :param filename:
        :return:
        """
        if 'mssql' in ipdict:
            printPink("crack sql serice  now...")
            print "[*] start crack sql serice  %s" % time.ctime()
            starttime = time.time()
            pool = Pool(threads)
            for ip in ipdict['mssql']:
                pool.apply_async(func=self.mssq1, args=(str(ip).split(':')[0], int(str(ip).split(':')[1])))
            pool.close()
            pool.join()

            print "[*] stop crack sql serice  %s" % time.ctime()
            print "[*] crack sql serice  done,it has Elapsed time:%s " % (time.time() - starttime)

            for i in xrange(len(self.result)):
                self.config.write_file(contents=self.result[i], filename=filename)


if __name__ == '__main__':
    import sys

    sys.path.append("../")
    from comm.config import *

    c = Config()
    ip_dict = {'mongodb': ['172.17.70.233:27017']}
    test = MyPoc(c)
    test.run(ip_dict, 50, filename="../result/test")
